package searchteam.client.ui.widgets;

import searchteam.client.beansMirrors.TeamMirror;
import searchteam.client.services.SearchTeamServices;
import searchteam.client.services.SearchTeamServicesAsync;
import searchteam.client.ui.callbacks.UpdateTeamCallback;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class UpdateTeamPopUp extends DialogBox {

	private Display display;
	private TeamMirror team;
	
	private PersonListBox chiefLB;
	
	SearchTeamServicesAsync services = GWT.create(SearchTeamServices.class);
	
	public UpdateTeamPopUp(Display d, TeamMirror t) {
		display = d;
		team = t;
		
		this.setTitle("Modification d'equipe");
		this.setText("Modification d'equipe");
		this.setPopupPosition(100, 100);
		
		Grid panel = new Grid(5,2);
		
		final TextBox nameTB = new TextBox();
		nameTB.setText(team.getName());
		final TextBox meaningTB = new TextBox();
		meaningTB.setText(team.getMeaning());
		final TextBox statusTB = new TextBox();
		statusTB.setText(team.getStatus());
		chiefLB = new PersonListBox(team.getTeamMaster());
		
		Button buttonOK = new Button("Enregistrer");
		buttonOK.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String name = nameTB.getText();
				String meaning = meaningTB.getText();
				String status = statusTB.getText();
				
				team.setName(name);
				team.setMeaning(meaning);
				team.setStatus(status);
				team.setTeamMaster(chiefLB.getSelectedPerson());
				services.updateTeam(team, new UpdateTeamCallback());
					
				display.getMainUI().display(new TeamMirrorDisplay(display, team));
					
				UpdateTeamPopUp.this.hide();
			}
		});
		Button buttonKO = new Button("Annuler");
		buttonKO.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				UpdateTeamPopUp.this.hide();
			}
		});
		
		panel.setWidget(0, 0, new Label("Nom : "));
		panel.setWidget(0, 1, nameTB);
		panel.setWidget(1, 0, new Label("Domaine : "));
		panel.setWidget(1, 1, meaningTB);
		panel.setWidget(2, 0, new Label("Statut : "));
		panel.setWidget(2, 1, statusTB);
		panel.setWidget(3, 0, new Label("Chef : "));
		panel.setWidget(3, 1, chiefLB);
		panel.setWidget(4, 0, buttonOK);
		panel.setWidget(4, 1, buttonKO);
		
		this.add(panel);
		this.show();
		
	}
}
